{% macro required_note() -%}
  <p class="req" title="{{ _('required') }}">* {{ _('Required fields') }}</p>
{%- endmacro %}

{% macro required() -%}
  <span class="req" title="{{ _('required') }}">*</span>
{%- endmacro %}

{% macro optional() -%}
  <span class="optional">{{ _('(optional)') }}</span>
{%- endmacro %}

{% macro pretty_field(field, label=None, tooltip=None, tag='li', req=None,
                      opt=False, choice=None, class='row', before='', validate=False) %}
  {% if choice == None %}
    {% set choice = field|is_choice_field %}
  {% endif %}
  {% if req == None %}
    {% set req = field.field.required %}
  {% endif %}
  {% set attrs = {'required': ''} if req and validate else {} %}

  {% if tag %}
    <{{ tag }} class="{{ class }}{{ ' error' if field.errors }}">
  {% endif %}
    <label class="{{ class }}{{ ' choice' if choice }}" for="{{ field.auto_id }}">
      {% if choice %}
        {{ field.as_widget(attrs=attrs) -}}
      {% endif %}
      {{- label or field.label }}

      {% if req %}
        {{ required() -}}
      {% endif %}

      {% if opt %}
        {{ optional() -}}
      {% endif %}

      {% set tooltip = tooltip or field.help_text %}
      {% if tooltip %}
        {{ tip(None, tooltip) }}
      {% endif %}
    </label>
    {{ before }}
    {% if not choice %}
      {{ field.as_widget(attrs=attrs) }}
    {% endif %}
    {{ field.errors }}
  {% if tag %}
    </{{ tag }}>
  {% endif %}
{% endmacro %}

{% macro tip(name, tip) %}
  {% if name %}
    <span class="label">{{ name }}</span>
  {% endif %}
  <span class="tip tooltip" title="{{ tip }}">?</span>
{% endmacro %}
